﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ET.DBCommon.Model;
using MySql.Data.MySqlClient;

namespace ET.DBCommon.DAL
{
    public class RecordtimeDAL
    {
        public static void ADD(List<RecordtimeModel> recordtimes)
        {
            foreach (var item in recordtimes)
            {
                string sql = @"INSERT INTO `recordtime`
                            (`ExamResultsTestQuestionID`,
                            `ExecTime`,
                            `InputTime`,
                            `DisplayText`,`IsTime`)
                            VALUES
                            (@ExamResultsTestQuestionID,
                            @ExecTime,
                            @InputTime,
                            @DisplayText,@IsTime)";

                MySqlParameter[] parms = {
                new MySqlParameter("@ExamResultsTestQuestionID", item.ExamResultsTestQuestionID ),
                new MySqlParameter("@ExecTime", item.ExecTime),
                new MySqlParameter("@InputTime", item.InputTime ),
                new MySqlParameter("@DisplayText", item.DisplayText),
                new MySqlParameter("@IsTime", item.IsTime ? 1 : 0 )
            };
                DbHelper.ExecuteNonQuery(sql, parms);
            }
        }

        public static List<RecordtimeModel> GetRecordtimeList(int ExamResultsTestQuestionID)
        {
            //string sql = "SELECT a.*,b.StepDescribe FROM Recordtime a inner join ExamresultsTestQuestionTaskStep b on a.ExamResultsTestStepID=b.id where a.ExamResultsTestQuestionID = " + ExamResultsTestQuestionID;
            string sql = "SELECT a.* FROM Recordtime a inner join ExamresultsTestQuestion b on a.ExamResultsTestQuestionID=b.id where a.ExamResultsTestQuestionID = " + ExamResultsTestQuestionID;
            DataSet ds = DbHelper.ExecuteDataset(sql);
            List<RecordtimeModel> list = new List<RecordtimeModel>();

            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    list.Add(GetTrainLateModel(dt.Rows[i]));
                }
            }
            return list;
        }

        internal static RecordtimeModel GetTrainLateModel(DataRow dr)
        {
            RecordtimeModel info = new RecordtimeModel();

            //info.ExamResultsTestStepID = Convert.ToInt32(dr["ExamResultsTestStepID"]);
            //info.ExamResultsTestStep = dr["StepDescribe"].ToString();
            info.ExecTime = dr["ExecTime"].ToString();
            info.InputTime = dr["InputTime"].ToString();
            info.DisplayText = dr["DisplayText"].ToString();
            info.IsTime = bool.Parse(dr["IsTime"].ToString());
            info.RecordTimeID = Convert.ToInt32(dr["RecordTimeID"]);
            return info;
        }
    }
}
